Shader "Anjum/Environment/MASTER_OPAQUE" {
	Properties{
		_MainTex("Main Texture", 2D) = "white" {}
		_SpecTex("Specular Texture", 2D) = "white" {}
		_NRMLMap("Normal Map", 2D) = "bump" {}
		_ColorOverlay("Color Overlay", Color) = (0,0,0,0)
		_SpecColor("Specular Color", Color) = (1,1,1,1)
		_SpecInten("Specular Intensity", float) = 1
		_Shininess("Shininess", float) = 10
	}
	
	SubShader{
		Pass{			
			Tags { "LightMode" = "ForwardBase" }
			
			CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			
			//In order to grab lighting information
			uniform float4 _LightColor0;
			
			////VARIABLES////
			uniform sampler2D _MainTex;
			uniform float4 _MainTex_ST;
			uniform sampler2D _SpecTex;
			uniform float4 _SpecTex_ST;
			uniform sampler2D _NRMLMap;
			uniform float4 _NRMLMap_ST;
			uniform float4 _ColorOverlay;
			uniform float4 _SpecColor;
			uniform float _SpecInten;
			uniform float _Shininess;
			
			////Data from model////
			struct vertexInput{
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
				float3 normal : NORMAL;
				float4 tangent : TANGENT;
			};
			
			////Data for fragment computations////
			struct vertexOutput{
				float4 pos : SV_POSITION;
				float4 tex : TEXCOORD0;
				float4 posWorld : TEXCOORD1;
				float3 normalDir : TEXCOORD2;
				float3 tangentWorld : TEXCOORD3;
				float3 binormalWorld : TEXCOORD4;
			};
			
			////Computing normals and the like////
			vertexOutput vert(vertexInput input){
				vertexOutput output;
				
				float4x4 modelMatrix = _Object2World;
				float4x4 modelMatrixInverse = _World2Object;
				
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
				output.tex = input.texcoord;
				output.posWorld = mul(modelMatrix, input.vertex);
				output.normalDir = normalize(mul(float4(input.normal, 0.0), modelMatrixInverse));
				output.tangentWorld = normalize(float3(mul(modelMatrix, float4(float3(input.tangent), 0.0))));
				output.binormalWorld = normalize(cross(output.normalDir, output.tangentWorld) * input.tangent.w);
				
				return output;
			}
			
			////Pixel shading! Lighting/color/etc////
			float4 frag(vertexOutput input) : COLOR{
			
				float4 finalColor = tex2D(_MainTex, input.tex.xy*_MainTex_ST.xy + _MainTex_ST.zw) + _ColorOverlay;
				
				
				////NORMAL////
				float4 encodedNormal = tex2D(_NRMLMap, input.tex.xy*_NRMLMap_ST.xy + _NRMLMap_ST.zw);
				float3 localCoords = float3(2.0 * encodedNormal.ag - float2(1.0), 0.0);
				localCoords.z = sqrt(1.0 - dot(localCoords, localCoords));
				
				float3x3 local2WorldTranspose = float3x3(
					input.tangentWorld,
					input.binormalWorld,
					input.normalDir);
				
				float3 normalDirection = normalize(mul(localCoords, local2WorldTranspose));
				float3 viewDirection = normalize(_WorldSpaceCameraPos - float3(input.posWorld));
				float3 lightDirection;
				float attenuation;
				
				if(0.0 == _WorldSpaceLightPos0.w){
					attenuation = 1.0;
					lightDirection = normalize(float3(_WorldSpaceLightPos0));
				} else {
					float3 vertexToLightSource = float3(_WorldSpaceLightPos0 - input.posWorld);
					float distance = length(vertexToLightSource);
					attenuation = 1.0/distance;
					lightDirection = normalize(vertexToLightSource);
				}
				
				
				////SPECULAR////
				float4 specularReflection;
				
				if(dot(normalDirection, lightDirection) < 0.0){
					specularReflection = float4(0);
				} else {
					specularReflection = attenuation * float4(_LightColor0) * float4(_SpecColor) * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess);
				}
				
				specularReflection *= _SpecInten;
				specularReflection *= tex2D(_SpecTex, input.tex.xy*_MainTex_ST.xy + _MainTex_ST.zw);
								
				finalColor += specularReflection;
				
				////RETURN////
				return finalColor;
			}
			
			
			ENDCG
		}
	}
}

